// kuaidi/pages/location/index.js
const App = getApp();
import Toast from '@vant/weapp/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    locationType: 0,
    keywords: '',
    wxapp: [],
    wxapp_id: '',
    area: [],
    areaIndex: 0,
    showArea: false
  },
  getWxappList() {
    let _this = this;
    App._get('wxapp/lists', {
      type: (_this.data.locationType + 1) == 1 ? 2 : 1,
      keywords: _this.data.keywords
    }, result => {
      let data = result.data.data
      _this.setData({
        wxapp: data
      })
    });
  },
  onTabsChange(e) {
    let _this = this
    _this.setData({
      locationType: e.detail.index
    })
    _this.getWxappList()
  },
  onAreaClick(e) {
    let _this = this
    // 设置当前选中的平台
    _this.setData({
      wxapp_id: e.currentTarget.dataset.wxapp_id
    })
    let wxapp = _this.data.wxapp
    let index = e.currentTarget.dataset.index
    for (let i in wxapp) {
      if (index == i) {
        wx.setStorageSync('wxapp', wxapp[i].setting)
        wx.setStorageSync('uniacid', _this.data.wxapp_id)
        break;
      }
    }
    //请求当前平台下的区域
    _this.getArea()
  },
  getArea() {
    let _this = this;
    App._get('wxapp/getArea/wxapp_id/' + _this.data.wxapp_id, {}, result => {
      console.log(result.data)
      if (result.data.length) {
        _this.setData({
          showArea: true,
          area: result.data
        })
        return false;
      }
      Toast({
        type: 'error',
        message: '暂无数据',
        onClose: () => {},
      });
    });
  },
  onCloseArea() {
    let _this = this;
    _this.setData({
      showArea: !_this.data.showArea
    })
  },
  onConfirmArea(e) {
    let _this = this;
    _this.onCloseArea()
    let area = e.detail.value
    if (area.length == 0) {
      return false;
    }
    wx.setStorageSync('area', JSON.stringify(e.detail.value))
    wx.setStorageSync('uniacid', _this.data.wxapp_id)
    Toast({
      type: 'success',
      message: '设置成功',
      onClose: () => {
        wx.navigateBack()
      },
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    _this.getWxappList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})